package com.swh.ribbon.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.BestAvailableRule;
import com.netflix.loadbalancer.ConfigurationBasedServerList;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PingUrl;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.netflix.loadbalancer.ServerListSubsetFilter;

/**
 * 可参考 RibbonClientConfiguration 
 * @author sangwenhao
 * @date 2017年1月9日
 */
//@Configuration
//@ComponentScan
public class MyDefaultRibbonConfig {
  @Bean
  public IRule ribbonRule() {
      return new BestAvailableRule();
  }

  @Bean
  public IPing ribbonPing() {
      return new PingUrl();
  }

  @Bean
  public ServerList<Server> ribbonServerList(IClientConfig config) {
      return new MyDefaultRibbonConfig.BazServiceList(config);
  }

  @Bean
  public ServerListSubsetFilter serverListFilter() {
      ServerListSubsetFilter filter = new ServerListSubsetFilter();
      return filter;
  }

  public static class BazServiceList extends ConfigurationBasedServerList {
      public BazServiceList(IClientConfig config) {
          super.initWithNiwsConfig(config);
      }
  }
}